﻿#include "HornedMage.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    HornedMage::HornedMage(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool HornedMage::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), Info->ViewRange);
    }

    void HornedMage::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && getEnvir()->getTime() < FearTime)
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 5000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist >= Info->ViewRange)
        {
            MoveTo(getTarget()->getCurrentLocation());
        }
        else
        {
            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }

        }
    }

    void HornedMage::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        bool ranged = getCurrentLocation() != getTarget()->getCurrentLocation() && !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 3);

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        ShockTime = 0;

        if (!ranged)
        {
            int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
            if (damage == 0)
            {
                return;
            }

            ActionTime = getEnvir()->getTime() + 500;
            AttackTime = getEnvir()->getTime() + AttackSpeed;

            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            if (getEnvir()->Random->Next(5) > 0)
            {
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
                    return;
                }

                ActionTime = getEnvir()->getTime() + 500;
                AttackTime = getEnvir()->getTime() + AttackSpeed;

                S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->TargetID = getTarget()->ObjectID;
                tempVar2->Type = 0;
                Broadcast(tempVar2);
                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 800, {getTarget(), damage, DefenceType::AC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
                tempVar3->ObjectID = ObjectID;
                tempVar3->Direction = getDirection();
                tempVar3->Location = getCurrentLocation();
                tempVar3->TargetID = getTarget()->ObjectID;
                tempVar3->Type = 1;
                Broadcast(tempVar3);

                TeleportTarget(4, 4);

                setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    bool HornedMage::TeleportTarget(int attempts, int distance)
    {
        for (int i = 0; i < attempts; i++)
        {
            Point *location;

            if (distance <= 0)
            {
                location = new Point(getEnvir()->Random->Next(getCurrentMap()->Width), getEnvir()->Random->Next(getCurrentMap()->Height));
            }
            else
            {
                location = new Point(getCurrentLocation()->X + getEnvir()->Random->Next(-distance, distance + 1), getCurrentLocation()->Y + getEnvir()->Random->Next(-distance, distance + 1));
            }

            if (getTarget()->Teleport(getCurrentMap(), location, true, Info->Effect))
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
                return true;
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
        }

        return false;
    }

    void HornedMage::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        target->Attacked(this, damage, defence);
    }

    void HornedMage::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(3, getCurrentLocation());

        for (int i = 0; i < targets.size(); i++)
        {
            targets[i]->Attacked(this, damage, defence);
        }
        return;
    }

    Packet *HornedMage::GetInfo()
    {
        return new S::ObjectMonster { ObjectID = ObjectID, Name = getName(), NameColour = NameColour, Location = getCurrentLocation(), Image = Info->Image, Direction = getDirection(), Effect = Info->Effect, AI = Info->AI, Light = Info->Light, Dead = Dead, Skeleton = Harvested, Poison = CurrentPoison, Hidden = getHidden(), Buffs = Buffs.Where([&] (std::any d)
        {
            return d->Visible;
        })->Select([&] (std::any e)
        {
            e::Type;
        }).ToList()};
    }
}
